#!/bin/bash

set -v

# Docker Hub's default build command seems to not handle .dockerignore correctly. Doing a simple build command
# ourselves works fine.

# This hook is called with the current directory set to the same as the Dockerfile, so we go back
# to top level.
cd ..

echo "Building images for $SOURCE_BRANCH"

docker build --build-arg source_branch=${SOURCE_BRANCH} -f "$DOCKERFILE_PATH" -t "$IMAGE_NAME" .

IFS=',' read -ra TAGS <<< "$DOCKER_TAG"
for tag in ${TAGS[@]}; do
  docker tag "$IMAGE_NAME" "${DOCKER_REPO}:${tag}"
done

# We always build zipkin-slim
echo Building zipkin-slim
docker build --build-arg source_branch=${SOURCE_BRANCH} -f "$DOCKERFILE_PATH" -t "openzipkin/zipkin-slim:${TAGS[0]}" --target zipkin-slim .
for tag in ${TAGS[@]:1}; do
  docker tag "openzipkin/zipkin-slim:${TAGS[0]}" "openzipkin/zipkin-slim:$tag"
done

# We always build test images formerly hosted on https://github.com/openzipkin/docker-zipkin
echo Building zipkin-ui
docker build --build-arg source_branch=${SOURCE_BRANCH} -f "$DOCKERFILE_PATH" -t "openzipkin/zipkin-ui:${TAGS[0]}" --target zipkin-ui .
for tag in ${TAGS[@]:1}; do
  docker tag "openzipkin/zipkin-ui:${TAGS[0]}" "openzipkin/zipkin-ui:$tag"
done

# We also build testing images to correspond with the server version to keep schemas up to date
for path in collector/kafka storage/cassandra storage/elasticsearch6 storage/elasticsearch7 storage/mysql; do
  image=zipkin-$(basename ${path})
  echo Building ${image}
  docker build --build-arg source_branch=${SOURCE_BRANCH} -f "docker/${path}/Dockerfile" -t "openzipkin/${image}:${TAGS[0]}" .
  for tag in ${TAGS[@]:1}; do
    docker tag "openzipkin/${image}:${TAGS[0]}" "openzipkin/${image}:$tag"
  done
done
